package com.example.good.config;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.good.entity.ExcelStuErrorDO;
import com.example.good.entity.ExportStudentDO;
import com.example.good.service.ClassService;
import com.example.good.service.StudentService;
import com.example.good.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @description: 学生导入监听器
 * @author: Peng houfu
 * @date: 2021-08-24 16:02
 **/
@Slf4j
public class StudentListener extends AnalysisEventListener<ExportStudentDO> {

    @Autowired
    private StudentService studentService;
    
    @Autowired
    private ClassService classService;
    
    List<ExportStudentDO> exportStudentDOS = new ArrayList<>();
    
    HttpServletResponse response;
    
    Map<Integer, String> map = new HashMap();
    
    private Result result = new Result();
    
    public StudentListener(StudentService studentService, ClassService classService, HttpServletResponse response){
        this.studentService = studentService;
        this.response = response;
        this.classService = classService;
    }

    /**
     * 一行一行的读取数据，此方法必写
     * 
     * @param exportStudentDO 要导入的数据实体类
     * @param analysisContext 监听器
     */
    @Override
    public void invoke(ExportStudentDO exportStudentDO, AnalysisContext analysisContext) {
        exportStudentDOS.add(exportStudentDO);
        exportStudentDOS.removeIf(Objects::isNull);
        
    }

    /**
     * 读取表头内容
     * 
     * @param headMap 表头数据
     * @param context 监听器
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        this.map = headMap;
        super.invokeHeadMap(headMap, context);
    }

    /**
     * 读取完后执行，此方法必写
     * 
     * @param analysisContext 监听器
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        Collection<String> values = map.values();
        if (CollUtil.isEmpty(values)){
            result = Result.error("模板错误");
            return;
        }else {
            if (!(values.contains("学号")) && values.contains("名字")){
                result = Result.error("模板错误");
                return;
            }
        }
        
        if (CollUtil.isEmpty(exportStudentDOS)){
            result = Result.error("空表或模板错误");
            return;
        }

        //获取列表总学生的id
        List<String> studentIds = exportStudentDOS.stream().map(ExportStudentDO::getStudentId).collect(Collectors.toList());
        List<ExcelStuErrorDO> errorLists = new ArrayList<>();
        ExportStudentDO exportStudentDO = null;
        Long maxTimes = null;
        
        for (ExportStudentDO exportStudent : exportStudentDOS){
            
        }

    }
    
    public ExportStudentDO checkTotalStudent(List<String> studentIds, ExportStudentDO exportStudentDO){
        
    }

    
}
